package com.cty.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @Auther: cty
 * @Date: 2020/2/12 16:50
 * @Description:
 * @version: 1.0
 */
@WebServlet(name = "NumServlet")
public class NumServlet extends HttpServlet {
    //覆写init初始化方法，将数据读取到ServletContext对象中
    @Override
    public void init() throws ServletException {
        System.out.println("初始化计数器");
        //获取文件中的计数器数据
        //获取文件路径
        String currentPath=this.getServletContext().getRealPath("/");
        File srcDir = new File(currentPath,"/nums");
        File srcDoc = new File(currentPath,"/nums/nums.txt");
        if(!srcDir.exists()){
            System.out.println(srcDir.getAbsolutePath()+"目录不存在，正在创建。。。");
            if(srcDir.mkdirs()){
                System.out.println("目录创建成功！");
            }
        }
        if(!srcDoc.exists()){
            try {
                System.out.println(srcDoc.getAbsolutePath()+"文件不存在，正在创建。。。");
                if(srcDoc.createNewFile()){
                    System.out.println("文件创建成功！");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String path=this.getServletContext().getRealPath("/nums/nums.txt");

        //声明流对象
        FileReader fr=null;
        BufferedReader br=null;
        try {
            fr=new FileReader(path);
            br=new BufferedReader(fr);
            String nums=br.readLine();

            if(nums!=null){
                this.getServletContext().setAttribute("nums", nums);
            }else{
                this.getServletContext().setAttribute("nums", 0+"");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                fr.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //覆写销毁方法，存储计数器到文件中
    @Override
    public void destroy() {
        System.out.println("保存计数器");
        //获取网页计数器
        int nums=Integer.parseInt((String) this.getServletContext().getAttribute("nums"));
//        int nums=(int) this.getServletContext().getAttribute("nums");  // 会出现空指针异常
        //获取文件路径
        String path=this.getServletContext().getRealPath("/nums/nums.txt");
        System.out.println(path);
        //声明流对象
        BufferedWriter bw=null;
        FileWriter fw=null;
        try {
            fw=new FileWriter(path);
            bw=new BufferedWriter(fw);
            bw.write(nums+"");
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                fw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
